package zhihui.mobile.ui.shop.cart;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zgm.utils.UiUtils;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.SkuInfo;
import zhihui.mobile.config.Config;
import zhihui.mobile.manager.shop.ShopManager;

/**
 * Created by 张公铭 on 2017/8/9 0009.
 */

public class ShopCartAdapter extends BaseAdapter
{
    private Context mContext;
    private LayoutInflater inflater;
    private List<SkuInfo> list = new ArrayList<>();
    private IShopNumberChangeListener listener;
    private boolean isAll = true;

    interface IShopNumberChangeListener
    {
        void addPrice(int price);

        void subtractPrice(int price);

        void updateCountNum(int num);

        void updateBuyNum(int num);
    }

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;

    public ShopCartAdapter(Context context)
    {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setAll(boolean all)
    {
        isAll = all;
    }

    public void setList(List<SkuInfo> list)
    {
        this.list = list;
    }

    public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener onCheckedChangeListener)
    {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    public void setListener(IShopNumberChangeListener listener)
    {
        this.listener = listener;
    }

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public Object getItem(int position)
    {
        return list.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        CheckBox cb_select = null;
        ImageView iv_icon, iv_add, iv_subtract = null;
        TextView tv_name = null;
        TextView tv_specification = null;
        TextView tv_num = null;
        TextView tv_price = null;
        RelativeLayout rl_specification, rl_gift = null;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.shopping_cart_item, null);
            cb_select = (CheckBox) convertView.findViewById(R.id.cb_shopping_cart_item_shop);
            iv_icon = (ImageView) convertView.findViewById(R.id.iv_shopping_cart_item_img);
            iv_add = (ImageView) convertView.findViewById(R.id.iv_shopping_cart_item_add);
            iv_subtract = (ImageView) convertView.findViewById(R.id.iv_shopping_cart_item_subtract);

            tv_name = (TextView) convertView.findViewById(R.id.tv_shopping_cart_item_name);
            tv_specification = (TextView) convertView.findViewById(R.id.tv_shopping_cart_item_specification);
            tv_price = (TextView) convertView.findViewById(R.id.tv_shopping_cart_item_price);
            tv_num = (TextView) convertView.findViewById(R.id.tv_shopping_cart_item_shop_num);

            rl_specification = (RelativeLayout) convertView.findViewById(R.id.rl_shopping_cart_item_specification);
            rl_gift = (RelativeLayout) convertView.findViewById(R.id.rl_shopping_cart_item_gift);

            ViewCache viewCache = new ViewCache();
            viewCache.cb_select = cb_select;
            viewCache.iv_icon = iv_icon;
            viewCache.iv_add = iv_add;
            viewCache.iv_subtract = iv_subtract;
            viewCache.tv_name = tv_name;
            viewCache.tv_num = tv_num;
            viewCache.tv_price = tv_price;
            viewCache.tv_specification = tv_specification;
            viewCache.rl_specification = rl_specification;
            viewCache.rl_gift = rl_gift;

            convertView.setTag(viewCache);
        } else
        {
            ViewCache viewCache = (ViewCache) convertView.getTag();
            cb_select = viewCache.cb_select;
            iv_icon = viewCache.iv_icon;
            iv_add = viewCache.iv_add;
            iv_subtract = viewCache.iv_subtract;
            tv_name = viewCache.tv_name;
            tv_num = viewCache.tv_num;
            tv_price = viewCache.tv_price;
            tv_specification = viewCache.tv_specification;
            rl_specification = viewCache.rl_specification;
            rl_gift = viewCache.rl_gift;
        }
        final SkuInfo skuInfo = list.get(position);
        ImageLoader.getInstance().displayImage(Config.FILE_URL + skuInfo.getThumbnail(), iv_icon);
        tv_name.setText(skuInfo.getTitle());
        int num = skuInfo.getNum() > skuInfo.getSalestock() ? skuInfo.getSalestock() : skuInfo.getNum();
        ShopManager.getInstance().getCartInfo().getSkus().get(position).setNum(num);
        tv_num.setText(num + "");
        final TextView finalTv_num = tv_num;
        final CheckBox finalCb_select = cb_select;
        iv_add.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                int curNum = Integer.parseInt(finalTv_num.getText().toString());
                if (curNum == skuInfo.getSalestock())
                {
                    UiUtils.ToastInCenter(mContext, "已是最大库存数量", 0);
                    return;
                }
                int num = Integer.parseInt(finalTv_num.getText().toString()) + 1;
                int sumNum = ShopManager.getInstance().getCartInfo().getCartNum();
                ShopManager.getInstance().getCartInfo().setCartNum(sumNum + 1);
                ShopManager.getInstance().getCartInfo().getSkus().get(position).setNum(num);
                finalTv_num.setText(num + "");
                listener.updateCountNum(1);
                if (finalCb_select.isChecked())
                {
                    listener.addPrice(skuInfo.getPrice());
                    listener.updateBuyNum(1);
                }
            }
        });


        final TextView finalTv_num1 = tv_num;
        final CheckBox finalCb_select1 = cb_select;
        iv_subtract.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (Integer.parseInt(finalTv_num1.getText().toString()) <= 1)
                {
                    return;
                }
                int num = Integer.parseInt(finalTv_num1.getText().toString()) - 1;
                int sumNum = ShopManager.getInstance().getCartInfo().getCartNum();
                ShopManager.getInstance().getCartInfo().setCartNum(sumNum - 1);
                ShopManager.getInstance().getCartInfo().getSkus().get(position).setNum(num);
                finalTv_num1.setText(num + "");
                listener.updateCountNum(-1);
                if (finalCb_select1.isChecked())
                {
                    listener.subtractPrice(skuInfo.getPrice());
                    listener.updateBuyNum(-1);
                }
            }
        });

        tv_price.setText(((double) skuInfo.getPrice() / 100) + "");
        tv_specification.setText(skuInfo.getSpecif().replace(",", "    "));
        rl_specification.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

            }
        });

        rl_gift.setVisibility(View.GONE);
        cb_select.setChecked(false);

        final TextView finalTv_num2 = tv_num;
        if (skuInfo.getSalestock() > 0)
        {
            cb_select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    if (isChecked)
                    {
                        ShopManager.getInstance().getCartInfo().getSkus().get(position).setSelect(true);
                        int num = Integer.parseInt(finalTv_num2.getText().toString());
                        listener.addPrice(skuInfo.getPrice() * num);
                        listener.updateBuyNum(num);
                    } else
                    {
                        ShopManager.getInstance().getCartInfo().getSkus().get(position).setSelect(false);
                        int num = Integer.parseInt(finalTv_num2.getText().toString());
                        listener.subtractPrice(skuInfo.getPrice() * num);
                        listener.updateBuyNum(num);
                    }
                }
            });
        }

        if (skuInfo.getSalestock() == 0)
        {
            cb_select.setChecked(false);
            cb_select.setEnabled(false);
        }

        if (isAll)
        {
            if (skuInfo.getSalestock() > 0)
            {
                cb_select.setChecked(true);
            }
        } else
        {
            cb_select.setChecked(false);
        }
        return convertView;
    }

    class ViewCache
    {
        public CheckBox cb_select;
        public ImageView iv_icon, iv_add, iv_subtract;
        public TextView tv_name, tv_specification, tv_num, tv_price;
        public RelativeLayout rl_specification, rl_gift;
    }
}
